//
//  MainViewController.swift
//  01MicroBlog
//
//  Created by Apple on 15/7/28.
//  Copyright © 2015年 heima. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 设置 tabBar 的按钮的颜色
        tabBar.tintColor = UIColor.orangeColor()
        
        // 增加子控制器
        addViewControllers()
        
    }
    
    // 重写viewDidAppear方法，将自定义按钮加到tabBar中
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)
        
        // 将加号按钮加到tabBar中
        tabBar.addSubview(composedButton)
        
        // 设置加号按钮的frame
        setUpcomposedBtnPosition()
        
    }
    
    // 设置加号按钮的frame
    private func setUpcomposedBtnPosition () {
        let width:CGFloat = tabBar.bounds.size.width / CGFloat(viewControllers!.count)
        let height:CGFloat = tabBar.bounds.size.height
        
        composedButton.frame = CGRect(x: 2*width , y: 0, width: width, height: height)
        print("\(composedButton.frame)")
    }
    
    
    // 给tabBar增加子控制器
    private func addViewControllers () {
        
        // 创建4个子控制器
        addChildViewControllers(HomeViewController(), title: "首页", normalImg: "tabbar_home", highlightedImg: "tabbar_home_highlighted")
        addChildViewControllers(MessageViewController(), title: "信息", normalImg: "tabbar_message_center", highlightedImg: "tabbar_message_center_highlighted")
        
        // 创建一个控制器，占着中间图片的位置
        addChildViewController(UIViewController())
        
        addChildViewControllers(DiscoveryViewController(), title: "发现", normalImg: "tabbar_discover", highlightedImg: "tabbar_discover_highlighted")
        addChildViewControllers(ProfileViewController(), title: "我", normalImg: "tabbar_profile", highlightedImg: "tabbar_profile_highlighted")
        
    }

    /**
    根据根控制器、标题、图片来创建导航控制器，将导航控制器加入到tabBarController中
    
    - parameter vc            : 根控制器
    - parameter title         : 导航栏的标题及tabBar的title
    - parameter normalImg     : 正常的图片
    - parameter highlightedImg: 高亮的图片
    */
    private func addChildViewControllers (vc:UIViewController, title:String, normalImg:String, highlightedImg: String) {
        
        // 设置子控制器的标题
        vc.title = title
        
        // 给自身的tabBar 设置图片
        vc.tabBarItem.image = UIImage(named: normalImg)
        vc.tabBarItem.selectedImage = UIImage(named: highlightedImg)
        
        // 创建导航控制器
        let nav = UINavigationController(rootViewController: vc)
        
        // 加入到子ChildViewController中
        addChildViewController(nav)
        
    }

    
    // 懒加载加号按钮
      private lazy var composedButton:UIButton = {
        
        // 创建加号按钮
        let composedBtn = UIButton()
        
        // 设置图片
        composedBtn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        composedBtn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        composedBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        composedBtn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 给按钮增加一个监听方法
        composedBtn.addTarget(self, action: "composedBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return composedBtn
    }()
    
    // 按钮的监听方法
    func composedBtnClick () {
        print("点击了加号按钮")
        
        let vc = UserAccount.userLogon ? ComposeViewController() : OAuthViewController()
        let nav =  UINavigationController(rootViewController: vc)
        presentViewController(nav, animated: true, completion: nil)
    }
    
}

    


